const defaultPs = {
  Program(node, env) {
    let item;
    for (item of node.body) {
      defaultPs.process(item, env);
    }
  },
  BlockStatement(node, env) {
    let item;
    for (item of node.body) {
      defaultPs.process(item, env);
    }
  },
  VariableDeclaration(node, env) {
    // node.declarations
    let item;
    const kind = node.kind;
    for (item of node.declarations) {
      defaultPs.process(item, env);
    }
  },
  VariableDeclarator(node, env) {
    defaultPs.process(node.id, env);
    if (node.init) defaultPs.process(node.init, env);
  },
  Identifier(node, env) {
    // name
  },
  Literal(node, env) {
    // raw
  },
  WhileStatement(node, env) {
    defaultPs.process(node.test, env);
    defaultPs.process(node.body, env);
  },
  BinaryExpression(node, env) {
    defaultPs.process(node.left, env);
    // operator
    defaultPs.process(node.right, env);
  },
  IfStatement(node, env) {
    defaultPs.process(node.test, env);
    defaultPs.process(node.consequent, env);
    defaultPs.process(node.alternate, env);
  },
  ExpressionStatement(node, env) {
    defaultPs.process(node.expression, env);
  },
  AssignmentExpression(node, env) {
    defaultPs.process(node.left, env);
    // operator: =
    defaultPs.process(node.right, env);
  },
  process(node, env) {
    const p = defaultPs[node.type];
    if (!p) {
      console.error('未处理的类型', node.type, node);
      return;
    }
    return p(node, env);
  },
};

class Walker {
  constructor(ps = defaultPs, env = {}) {
    this.ps = ps;
    this.env = env;
  }

  walk(tree) {
    this.ps.init && this.ps.init(this.env);
    return this.ps.process(tree, this.env);
  }
};

export default Walker;
